--
-- Author: shunguo.chen
-- Date: 2018-01-11 18:31:29
--
-- CosplayInfo 角色扮演选人信息

local CosplayChoice = import(".CosplayChoice")

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

function ClassRef:ctor(data)
	if data then
		self:init(data)
	end
end

-- message RollPlayInfo {
--    int32 targetID = 1; //rolePlayConfig的ID
--    string teamID = 2;
--    int32 leftSeconds = 10; //剩余选择时间
--    repeated RollChoice rollChoices  = 20; //选人列表
--    RollPlayChoiceStatus status = 30;
-- }
function ClassRef:init(data)
	self.teamID = data.teamID
	self.status = data.status
	self.leftSeconds = data.leftSeconds

	self._initTime = app:getTime()

	self._choices = self._choices or {}
	local rollNum = #data.rollChoices
	while #self._choices > rollNum do
		table.remove(self._choices, #self._choices)
	end

	self._myChoice = nil
	for i,v in ipairs(data.rollChoices) do
		local choice = self._choices[i]
		if not choice then
			choice = CosplayChoice.new()
			self._choices[i] = choice
		end
		choice:init(v)
		if nil == self._myChoice and choice:isMe() then
			self._myChoice = choice
		end
	end

	-- target conf
	self.preTargetID = self.targetID
	self.targetID = data.targetID
	if self.preTargetID ~= self.targetID then
		-- 目标不一样 更新配置
		local conf = GD:queryCosplayTargetConf(self.targetID)
		self._roleIds = table.tonumber(string.split(conf.rolePool, ";"))
		self._conf = conf

		if self.targetID == Enums.TeamTarget.theatre then
			-- 歌剧院由三场pve（theatrePool表）+ 一场pvp组成
			local theatreTargetID = app:sendMsg("TheatreModel", "getTheatreTargetId")
			print("theatreTargetID", theatreTargetID)
			if theatreTargetID then
				self._type = Enums.CosplayType.pve
				local conf = GD:queryTheatreTargetConf(theatreTargetID)
				-- self._roleIds = table.tonumber(string.split(conf.rolePool, ";"))
				self._conf = conf
			else
				self._type = Enums.CosplayType.pvp5
			end
		else
			local targetConf = GD:queryTeamTargetById(self.targetID)
			if targetConf.vsType == "1" then
				self._type = Enums.CosplayType.pve
				if targetConf.maxNum == "10" then
					self._type = Enums.CosplayType.pve10
				end
			else
				if targetConf.maxNum == "1" then
					self._type = Enums.CosplayType.pvp1
				else
					self._type = Enums.CosplayType.pvp5
				end
			end
		end
	end
end

-- 剩余时间
function ClassRef:getRestTime()
	return self.leftSeconds - math.floor(app:getTime() - self._initTime)
end

function ClassRef:refreshInitTime()
    self._initTime = app:getTime()
end

-- 是我的队伍
function ClassRef:isMyTeam()
	return self._myChoice ~= nil
end

-- 自己是否已经选择
function ClassRef:hasMyselfChoosed()
	if self._myChoice then
		return self._myChoice:isChoose()
	end
	return false
end

function ClassRef:getMyChooseRole()
	if self._myChoice then
		return self._myChoice:getRole()
	end
end

function ClassRef:getType()
	return self._type
end

-- 对战双方队伍是否可以重复选择英雄，我方队伍不能重复选择英雄
function ClassRef:isRepeated()
	return self._conf.repeated == "1"
end

function ClassRef:getChoices()
	return self._choices
end

function ClassRef:getRoleIds()
	return self._roleIds
end

-- pve模式target是一个boss
function ClassRef:getBossTarget()
	local ret = {}
	ret.targetName = L(self._conf.dungeonTitle)
	ret.name = L(self._conf.dungeonBoss)
	ret.icon = Res.npcIconPath .. self._conf.dungeonIcon .. ".png"
	return ret
end

function ClassRef:isFinish()
	return self.status == "finished"
end


return ClassRef